#include<bits/stdc++.h>
#define pb push_back
#define rep(x,l,r) for(int x=l;x<=r;x++)

using namespace std;
const int N=2e5+10,M=1<<15,K=1<<5;

int n,m,c,all,a[N],lim,x,prt[N],az;vector<int>g[N];
void dfs(int x,int fa){az++;for(int y:g[x])if(y!=fa&&((lim>>a[y])&1))dfs(y,x);}
int ans[K][N];void add(int x,int fa){ans[lim][x]=az;for(int y:g[x])if(y!=fa&&((lim>>a[y])&1))add(y,x);}

int main(){

	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);

	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin>>n>>m>>c;
	all=(1<<c)-1;rep(i,1,n)cin>>a[i];rep(i,2,n)cin>>x,prt[i]=x,g[x].pb(i),g[i].pb(x);
	if(n<=5000&&m<=5000){while(m--){cin>>lim>>x;if(!((lim>>a[x])&1)){cout<<1<<"\n";continue;}az=0;dfs(x,0);cout<<az<<"\n";}return 0;}
	rep(i,0,all){lim=i;rep(j,1,n)if(((lim>>a[j])&1)&&(j==1||!((lim>>a[prt[j]])&1)))az=0,dfs(j,prt[j]),add(j,prt[j]);}
	while(m--)cin>>lim>>x,cout<<ans[lim][x]<<"\n";

	return 0;
}
